<?php
/**
 * Users.php
 *
 * PHP version 5
 *
 * LICENSE: This source file is subject to version 2.0 of the Apache license
 * that is available through the world-wide-web at the following URI:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * @category Controller
 * @package  Controller
 * @author   Alex Kopp <koppa@rpi.edu>
 * @license  http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
 * @link     http://http://code.google.com/p/tri-chi/
 */

/**
 * Users
 *
 * The model that manages all the Users of the GLOrganizer System
 * 
 * The followings are the available columns in table 'users':
 * 
 * @property string $username
 * @property string $password
 * @property string $first_name
 * @property string $last_name
 * @property string $role
 *
 * The followings are the available model relations:
 * @property Comments[] $comments
 * @property Introductions[] $introductions
 * @property Ratings[] $ratings
 *
 * @category Controller
 * @package  Controller
 * @author   Alex Kopp <koppa@rpi.edu>
 * @license  http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
 * @link     http://http://code.google.com/p/tri-chi/
 */
class Users extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * 
     * @param unknown_type $className Class Name
     * 
     * @return Members the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * Table Name
     * 
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'users';
    }

    /**
     * Validation Rules
     * 
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array(
                'username, password, first_name, last_name, role',
                'required'
            ),
            array('username, first_name, last_name', 'length', 'max'=>32),
            array('role', 'length', 'max'=>13),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('username, password, first_name, last_name, role', 'safe', 'on'=>'search'),
            array('username', 'unique'),
        );
    }

    /**
     * Relations
     * 
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'comments' => array(self::HAS_MANY, 'Comments', 'user_username'),
            'introductions' => array(self::HAS_MANY, 'Introductions', 'user_username'),
            'ratings' => array(self::HAS_MANY, 'Ratings', 'user_username'),
        );
    }

    /**
     * Attribute Labels
     * 
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'username' => 'Username',
            'password' => 'Password',
            'first_name' => 'First Name',
            'last_name' => 'Last Name',
            'role' => 'Role',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * 
     * @return CActiveDataProvider data provider that returns models based on search
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('username', $this->username, true);
        //$criteria->compare('password', $this->password, true);
        $criteria->compare('first_name', $this->first_name, true);
        $criteria->compare('last_name', $this->last_name, true);
        $criteria->compare('role', $this->role, true);

        return new CActiveDataProvider(
            $this, array(
                'criteria'=>$criteria,
            )
        );
    }

    /**
     * Role Options
     * 
     * @return array
     */
    public function getRoleOptions()
    {
        return array('member'=>'Member', 'admin'=>'Administrator');
    }
}